package com.david.rabbitmq.spring.xml;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import java.io.UnsupportedEncodingException;

/**
 * @author David
 * @version V1.0
 * @Package com.david.rabbitmq.spring.xml
 * @date 2021/7/17 11:50
 */
public class RabbitmqConsumerApplication {
    public static void main(String[] args) throws UnsupportedEncodingException {
        AbstractApplicationContext context = new GenericXmlApplicationContext("classpath:/rabbit-context.xml");
        final RabbitTemplate template = context.getBean(RabbitTemplate.class);
        while(true){
            final Message message = template.receive("queue.q1");
            if (message==null){
                break;
            }
            // 拉消息模式
            System.out.println(new String(message.getBody(), message.getMessageProperties().getContentEncoding()));
        }
        context.close();
    }
}
